<?php
namespace Merch\Controller;

use Common\Controller\AdminbaseController;

class AdminCommentsController extends AdminbaseController {

    protected $comments_model;

    public function _initialize() {
        parent::_initialize();
        $this->comments_model=D("Merch/MerchComments");
    }


    public function index(){

        $where=array("status"=>array('neq',-1));

        $count=$this->comments_model->where($where)->count();

        $page=$this->page($count,20);

        $comments=$this->comments_model->where($where)
        ->order("create_time desc")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();

        $this->comments_model
        ->alias("a")
        ->join("__USERS__ c ON a.uid = c.id")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");

        $this->comments_model->field('a.*,c.user_nicename');

        $comments=$this->comments_model->select();

        // $this->assign("page",$page->show("default"));
        $this->assign("page", $page->show('Admin'));
        $this->assign("comments",$comments);
        $this->display();
    }

    public function delete(){
        if(isset($_GET['id'])){
            $id = intval(I("get.id"));
            if ($this->comments_model->where("id=$id")->delete()!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
        if(isset($_POST['ids'])){
            $ids=join(",",$_POST['ids']);
            if ($this->comments_model->where("id in ($ids)")->delete()!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

    public function check(){
        if(isset($_POST['ids']) && $_GET["check"]){
            $data["status"]=1;

            $ids=join(",",$_POST['ids']);

            if ( $this->comments_model->where("id in ($ids)")->save($data)!==false) {
                $this->success("审核成功！");
            } else {
                $this->error("审核失败！");
            }
        }
        if(isset($_POST['ids']) && $_GET["uncheck"]){

            $data["status"]=0;
            $ids=join(",",$_POST['ids']);
            if ( $this->comments_model->where("id in ($ids)")->save($data)!==false) {
                $this->success("取消审核成功！");
            } else {
                $this->error("取消审核失败！");
            }
        }
    }

}